package vader.util;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

public class CommonUtils {
    /*
        下面是工具方法
     */
    public static boolean notNull(Object object) {
        return notNull(object, false);

    }

    public static boolean notNull(Object object, boolean o) {
        if (object == null) {
            return false;
        }
        if (object instanceof String) {
            return StringUtils.isNotEmpty(String.valueOf(object));
        }
        if (object instanceof Number) {
            Number n = (Number) object;
            if (o && n.doubleValue() == 0.0d) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNull(Object object) {
        return isNull(object, false);
    }

    public static boolean isNull(Object object, boolean o) {
        if (object == null) {
            return true;
        }
        if (object instanceof String) {
            return StringUtils.isEmpty(String.valueOf(object));
        }
        if (object instanceof Number) {
            Number n = (Number) object;
            if (o && n.doubleValue() == 0.0d) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasField(Class c, String fieldName){
        Field[] fields = c.getDeclaredFields();
        for (Field f : fields) {
            if (fieldName.equals(f.getName())) {
                return true;
            }
        }
        return false;
    }
}
